﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SceneEvidenceAPISDK
{
    public class HttpTools
    {
        /// <summary>
        /// API根地址
        /// </summary>
        public readonly static string _baseAddr = "http://localhost:5055/api/";
        //public readonly static string _baseAddr = "http://47.114.61.176:8018/api/";
        /// <summary>
        /// 全局Token
        /// </summary>
        public static string accessToken = "";
        public static string refreshToken = "";
        /// <summary>
        /// Post
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static T Post<T>(dynamic query, string apiAddr)
        {
            try
            {
                RestSharp.RestClient restClient = new RestClient(_baseAddr);
                RestSharp.RestRequest restRequest = new RestRequest(apiAddr, Method.POST);
                if (!string.IsNullOrWhiteSpace(accessToken))
                    restRequest.AddHeader("Authorization", string.Format("Bearer {0}", accessToken));
                restRequest.AddParameter("application/json", JsonConvert.SerializeObject(query), ParameterType.RequestBody);
                var response = restClient.Execute(restRequest);
                return JsonConvert.DeserializeObject<T>(response.Content);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static T Delete<T>(dynamic query, string apiAddr)
        {
            try
            {
                RestSharp.RestClient restClient = new RestClient(_baseAddr);
                RestSharp.RestRequest restRequest = new RestRequest(apiAddr, Method.DELETE);
                restRequest.AddHeader("Authorization", string.Format("Bearer {0}", accessToken));
                restRequest.AddParameter("application/json", JsonConvert.SerializeObject(query), ParameterType.RequestBody);
                var response = restClient.Execute(restRequest);
                return JsonConvert.DeserializeObject<T>(response.Content);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Get
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static T Get<T>(dynamic query, string apiAddr)
        {
            try
            {
                RestSharp.RestClient restClient = new RestClient(_baseAddr);
                RestSharp.RestRequest restRequest = new RestRequest(apiAddr, Method.GET);
                restRequest.AddHeader("Authorization", string.Format("Bearer {0}", accessToken));
                restRequest.AddParameter("application/json", JsonConvert.SerializeObject(query), ParameterType.RequestBody);
                var response = restClient.Execute(restRequest);
                return JsonConvert.DeserializeObject<T>(response.Content);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static T Get<T>(string apiAddr)
        {
            try
            {
                RestSharp.RestClient restClient = new RestClient(_baseAddr);
                RestSharp.RestRequest restRequest = new RestRequest(apiAddr, Method.GET);
                if (!string.IsNullOrWhiteSpace(accessToken))
                    restRequest.AddHeader("Authorization", string.Format("Bearer {0}", accessToken));
                var response = restClient.Execute(restRequest);
                return JsonConvert.DeserializeObject<T>(response.Content);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Put
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static T Put<T>(dynamic query, string apiAddr)
        {
            try
            {
                RestSharp.RestClient restClient = new RestClient(_baseAddr);
                RestSharp.RestRequest restRequest = new RestRequest(apiAddr, Method.PUT);
                restRequest.AddHeader("Authorization", string.Format("Bearer {0}", accessToken));
                if (query != null && !string.IsNullOrWhiteSpace(query))
                    restRequest.AddParameter("application/json", JsonConvert.SerializeObject(query), ParameterType.RequestBody);
                var response = restClient.Execute(restRequest);
                return JsonConvert.DeserializeObject<T>(response.Content);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
